package com.tjbknb.ecui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;


public class Data {
	String FILENAME = "data.bin";
	savedData data;


	public Data(){
		File f = new File(FILENAME);
		if(f.exists()){
			try {
				FileInputStream fis = new FileInputStream(f);
				ObjectInputStream os = new ObjectInputStream(fis);
				data = (savedData)os.readObject();
				fis.close();
				os.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (StreamCorruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}else{
			data = new savedData();
			ObjectOutputStream os;
			try {
				os = new ObjectOutputStream(new FileOutputStream(FILENAME));
				os.writeObject(data);
				os.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}	
		}
	}

	//adds last time the user was impatient to times array
	public void addTime(){
		data.times.add(new Timestamp(System.currentTimeMillis()));

		if(data.times.size() > 10){
			data.times.remove(0);
		}
	}

	//adds new min frequency to process list if smaller than current value
	public void updateProcessList(String p, int f){
		if(data.processList.containsKey(p)){
			if(data.processList.get(p) > f){
				data.processList.put(p, f);
			}
		}
		else{
			data.processList.put(p, f);
		}
	}

	//increase all the frequencies in the hashtable 
	public void increaseFreqs(){
		for (Integer val : data.processList.values() ) {
			val = val + (val / 2); // increase all mins by 50%
		}
	}

	//save long term data
	public void saveData(){
		ObjectOutputStream os;
		try {
			os = new ObjectOutputStream(new FileOutputStream(FILENAME));
			os.writeObject(data);
			os.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//public Timestamp getDeltaStopTime(){
	//	return data.delta_stop_time;
	//}

	public boolean userIsReallyImpatient(){
		if(data.times.size() == 10){
			long timespan = data.times.get(9).getTime() - data.times.get(0).getTime();
			
			if(timespan < data.reallyImp){
				return true;
			}
			else{
				return false;
			}
			
		}
		else{
			return false;
		}
	}
	
	public void setDelta(){
		data.delta += data.delta_interval;
		data.delta_stop_time = new Date((new Date().getTime()) + data.delta_time);
		
		if(data.delta > data.delta_interval * 3){
			increaseFreqs();
		}
	}

	
	//smaller inner class that is actually saved for long term storage
	public class savedData{
		ArrayList<Date> times;
		Hashtable<String, Integer> processList;
		/////VARIABLE SETTINGS POSSIBLY
		int dec_amt = 100000; //100 MHz
		int less_dec_amount = 25000; //25Mhz
		int dec_time = 10000; //10 seconds	
		int reallyImp = 900000; //15 mins
		int delta_interval = 150000;
		long delta_time = 108000000;
		/////
		int delta = 0;
		Date delta_stop_time = new Date();

		public savedData(){
			times = new ArrayList<Date>();
			processList = new Hashtable<String, Integer>();
			delta = 0;
		}

	}
}


